<?php
declare (strict_types=1);

namespace app\admin\service\advertiser;

use app\admin\model\advertiser\AdvertiserTask;
use app\admin\model\advertiser\AdvertiserTaskLog;
use app\BaseService;
use think\response\Json;

/**
 * 广告主推广任务
 */
class AdvertiserTaskService extends BaseService
{

    /**
     * 广告主推广任务列表
     * @param array $params
     * @return Json
     */
    public function taskList(array $params): Json
    {
        $where[] = ['closed', '=', 0];
        //搜索任务名称
        if ($params['task_name']) {
            $where[] = ['task_name', 'like', '%' . $params['task_name'] . '%'];
        }
        //搜索广告主id
        if ($params['ad_user_id']) {
            $where[] = ['ad_user_id', '=', $params['ad_user_id']];
        }
        //搜索类型
        if ($params['type'] and in_array($params['type'], [1, 2])) {
            $where[] = ['type', '=', $params['type']];
        }
        //搜索状态
        if ($params['status'] and in_array($params['status'], [1, 2, 3, 4, 5, 6])) {
            $where[] = ['status', '=', $params['status']];
        }
        //创建时间筛选
        if ($params['start_time'] && $params['end_time']) {
            $start_time = (int)$params['start_time'];
            $end_time = (int)$params['end_time'];
            $where[] = ['create_time', 'between', [$start_time, $end_time]];
        }
        //获取列表
        $data = AdvertiserTask::getWechatList($where, $params['page'], $params['limit'], 'id desc');
        return jsonReturn(1, $data);
    }

    /**
     * 广告主推广任务审核
     * @param array $params
     * @return Json
     */
    public function taskReview(array $params): Json
    {
        //判断参数
        if (!in_array($params['status'], [3, 4])) {
            return jsonReturn(0, [], '状态参数错误');
        }
        //获取当前任务
        $where = ['id' => $params['task_id'], 'status' => 2, 'closed' => 0];
        $Model = AdvertiserTask::where($where)->find();
        if (!$Model) {
            return jsonReturn(0, [], '任务不存在');
        }
        //修改当前状态
        $data = ['status' => $params['status'], 'review_time' => time()];
        //判断是否为审核通过
        if ($params['status'] == 4 && $Model->is_auto_online == 1) {
            $data['status'] = 5;
        }
        $res = $Model->save($data);
        if ($res) {
            return jsonReturn(1, []);
        }
        return jsonReturn(0, []);
    }

    /**
     * 广告主推广任务上下架
     * @param int $task_id
     * @param int $enable
     * @return Json
     */
    public function taskEnable(int $task_id, int $enable): Json
    {
        //判断参数
        if (!in_array($enable, [5, 6])) {
            return jsonReturn(0, [], '参数错误');
        }
        //查询广告主用户
        $where = ['id' => $task_id, 'closed' => 0];
        $Model = AdvertiserTask::where($where)->find();
        if (!$Model) {
            return jsonReturn(0, [], '广告主推广任务不存在');
        }
        //编辑
        $params['status'] = $enable;
        $params['update_time'] = time();
        $res = $Model->save($params);
        if ($res) {
            return jsonReturn(1, []);
        }
        return jsonReturn(0, [], '操作失败');
    }

    /**
     * 广告主推广任务排序列表
     * @param array $params
     * @return Json
     */
    public function sortList(array $params): Json
    {
        $where[] = ['closed', '=', 0];
        //搜索类型
        if ($params['type'] and in_array($params['type'], [1, 2])) {
            $where[] = ['type', '=', $params['type']];
        }
        //搜索状态
        if ($params['status'] and in_array($params['status'], [1, 2, 3, 4, 5, 6])) {
            $where[] = ['status', '=', $params['status']];
        }
        //获取列表
        $orderby = 'orderby asc,id desc';
        $data = AdvertiserTask::getSortList($where, $params['page'], $params['limit'], $orderby);
        return jsonReturn(1, $data);
    }

    /**
     * 广告主推广任务设置排序值
     * @param int $task_id
     * @param int $orderby
     * @return Json
     */
    public function taskSort(int $task_id, int $orderby): Json
    {
        //获取当前任务
        $where = ['id' => $task_id, 'closed' => 0];
        $Model = AdvertiserTask::where($where)->find();
        if (!$Model) {
            return jsonReturn(0, [], '任务不存在');
        }
        //设置排序
        $where = ['type' => $Model->type];
        AdvertiserTask::setOrderBy($Model, $where, $orderby);
        //修改排序值
        $data = ['orderby' => $orderby, 'update_time' => time()];
        $res = $Model->save($data);
        if ($res) {
            return jsonReturn(1, []);
        }
        return jsonReturn(0, []);
    }

    /**
     * 广告主推广任务明细记录
     * @param array $params
     * @return Json
     */
    public function taskLog(array $params): Json
    {
        $where = [];
        //搜索任务名称
        if ($params['task_name']) {
            $where[] = ['b.task_name', 'like', '%' . $params['task_name'] . '%'];
        }
        //搜索任务名称
        if ($params['unionid']) {
            $where[] = ['c.unionid', '=', $params['unionid']];
        }
        //创建时间筛选
        if ($params['start_time'] && $params['end_time']) {
            $start_time = (int)$params['start_time'];
            $end_time = (int)$params['end_time'];
            $where[] = ['a.create_time', 'between', [$start_time, $end_time]];
        }
        //获取列表
        $data = AdvertiserTaskLog::getList($where, $params['page'], $params['limit'], 'a.id desc');
        return jsonReturn(1, $data);
    }

}
